﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PeopleSimulator
{
    public partial class RunForm : Form
    {
        BindingList<Detail> _Details = null;
        Loli.Library.WinIo.Action _Action = new Loli.Library.WinIo.Action();
        private CancellationTokenSource cts = null;


        public Script Script
        {
            get;
            set;
        }

        private void DisabledControls(bool isDisabled)
        {
            foreach (Control control in this.Controls)
            {
                if (control is Button || control is TextBox || control is CheckBox)
                {
                    control.Enabled = !isDisabled;
                }
            }
        }


        public RunForm()
        {
            InitializeComponent();
            this._Details = new BindingList<Detail>();
            this.lbList.DataSource = this._Details;
            this.lbList.DisplayMember = "DisplayString";

            this.cts = new CancellationTokenSource();


        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            this._Details.Clear();
            foreach (var item in this.Script.Details)
            {
                this._Details.Add(item);
            }
            _Action.Initialize();
            // this.Run();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            _Action.Shutdown();
            this.Hide();
        }

        private void butClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Run(bool isLoop)
        {
            this.DisabledControls(true);
            Task task = new Task(delegate()
            {
                for (int i = 5; i >= 0; i--)
                {
                    this.Invoke((UpdateInterface)delegate
                    {
                        this.butRun.Text = i + "秒";
                    });
                    Thread.Sleep(1000);
                }
                this.Invoke((UpdateInterface)delegate
                {
                    this.butRun.Text = "停止";
                    this.butRun.Enabled = true;
                });

                if (isLoop)
                {
                    while (true)
                    {
                        LoopDetail();
                    }
                }
                else
                {
                    LoopDetail();
                }

                this.Invoke((UpdateInterface)delegate
                {
                    this.butRun.Text = "运行";
                    this.DisabledControls(false);
                });
            }, cts.Token);

            task.Start();
        }

        private void LoopDetail()
        {
            foreach (var detail in this._Details)
            {

                if (cts.Token.IsCancellationRequested)
                {
                    throw new OperationCanceledException(cts.Token);
                }

                this.Invoke((UpdateInterface)delegate
                {
                    this.lbList.SelectedItem = detail;
                });

                if (detail.KeyType == KeyTypes.KeyDown)
                {
                    _Action.KeyDown(detail.KeyCodes);
                }
                else if (detail.KeyType == KeyTypes.KeyUp)
                {
                    _Action.KeyUp(detail.KeyCodes);
                }

                if (detail.Deley > 0)
                {
                    Thread.Sleep(detail.Deley);
                }
            }
        }

        private void butRun_Click(object sender, EventArgs e)
        {
            if (butRun.Text == "停止")
            {
                cts.Cancel();
                this.DisabledControls(false);
                this.butRun.Text = "运行";
            }
            else
            {
                this.Run(cbLoop.Checked);
            }
        }
    }
}
